home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / fstab / fstab3.c < prev    next >
C/C++ Source or Header  |  1996-03-01  |  5KB  |  213 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <dirent.h>
  8. #include "../misc/misc.h"
  9. #include "fstab.h"
  10. #include "fstab.m"
  11.  
  12. extern FSTAB_HELP_FILE help_mount;
  13. static FSTAB_HELP_FILE help_swap ("swap");
  14.  
  15. PRIVATE void FSTAB::fixroot(FSTAB_ENTRY *root, char *status)
  16. {
  17.     char *errmsg = NULL;
  18.     struct stat roots;
  19.     if (stat("/",&roots) < 0){
  20.         errmsg = "directory /";
  21.     }else{
  22.         DIR *dp = opendir("/dev");
  23.         if (dp == NULL){
  24.             errmsg = "/dev";
  25.         }else{
  26.             char name[PATH_MAX+1];
  27.             strcpy(name,"/dev/");
  28.             struct dirent *dir;
  29.             while ((dir = readdir(dp))!=NULL) {
  30.                 strcpy(name+5,dir->d_name);
  31.                 struct stat s;
  32.                 if (stat(name,&s) < 0){
  33.                     errmsg = name;
  34.                     break;
  35.                 }else if ((s.st_mode & S_IFMT) == S_IFBLK
  36.                     && s.st_rdev == roots.st_dev){
  37.                     if (strcmp(root->getsource(),name)!=0){
  38.                         root->setsource (name);
  39.                         strcat (status
  40.                             ,MSG_U(N_ROOTDEF
  41.                                 ,"The root device has been changed to "));
  42.                         strcat (status,name);
  43.                         strcat (status,"\n\n");
  44.                     }
  45.                     break;
  46.                 }
  47.             }
  48.             closedir(dp);
  49.         }
  50.     }
  51.     if (errmsg){
  52.         xconf_error (MSG_U(E_CANTVLD,"Can't validate the root entry\n"
  53.             "of your /etc/fstab file for the following\n"
  54.             "reason:\n"
  55.             "\n"
  56.             "%s: %s"),errmsg,strerror(errno));
  57.     }
  58. }
  59.  
  60. /*
  61.     Do some sanity check on one entry of /etc/fstab
  62.     Return -1 if any error that can't be fixed.
  63. */
  64. PUBLIC int FSTAB_ENTRY::check()
  65. {
  66.     /* #Specification: fstab / entry / checking
  67.         fsconf make sure the entry has a valid (existing mount point).
  68.         It check if it exist and it check if it is a directory.
  69.  
  70.         If it does not exist, the user is allowed to create it.
  71.     */
  72.     int ret = 0;
  73.     if (is_valid() && !is_swap()){
  74.         struct stat st;
  75.         const char *path = mpoint.get();
  76.         ret = -1;
  77.         if (path[0] == '\0'){
  78.             xconf_error (MSG_U(E_NOMNTPT
  79.                 ,"You must specify a "
  80.                 "mount point(directory)\n"
  81.                 "for entry %s\n"),source.get());
  82.         }else if (stat(path,&st)==-1){
  83.             char buf[1000];
  84.             sprintf (buf,MSG_U(Q_EXISTMNT,"Mount point %s\n"
  85.                  "does not exist.\n"
  86.                  "Do you want to create it ?\n")
  87.                 ,path);
  88.             if (xconf_yesno(MSG_U(T_CANTMOUNT,"Can't mount")
  89.                 ,buf
  90.                 ,help_mount) == MENU_YES){
  91.                 mkdir (path,0755);
  92.                 ret = 0;
  93.             }
  94.         }else if (!S_ISDIR(st.st_mode)){
  95.             xconf_error (MSG_U(E_ISNOTDIR
  96.                 ,"%s is not a directory\n"
  97.                  "please pick a different mount point\n")
  98.                 ,path);
  99.         }else{
  100.             ret = 0;
  101.         }
  102.     }
  103.     return ret;
  104. }
  105.  
  106. /*
  107.     Return the root device
  108. */
  109. PUBLIC const char *FSTAB::getrootdev()
  110. {
  111.     const char *ret = "";
  112.     for (int i=0; i<getnb(); i++){
  113.         FSTAB_ENTRY *e = getitem(i);
  114.         const char *mpoint = e->getmpoint();
  115.         if (strcmp(mpoint,"/")==0){
  116.             ret = e->getsource();
  117.             break;
  118.         }
  119.     }
  120.     return ret;
  121. }
  122.  
  123. /*
  124.     Make sure the /etc/fstab file contain minimally accurate entries.
  125.     The caller will have to save it by checking was_modified().
  126. */
  127. PUBLIC void FSTAB::check()
  128. {
  129.     const char *NO_SWAP_NEEDED = "# Please no swap";
  130.     /* #Specification: /etc/fstab / minimal check
  131.         The /etc/fstab is checked to make sure is contain
  132.         the basic entries requiered to run successfully
  133.         a Linux system.
  134.  
  135.         -It check for the root fs, both for presence and
  136.          accuracy. For example, if the user reorganise its
  137.          drive (linux was hdb and is now hda), or copy
  138.          a working (configured) installation to another
  139.          disk/partition, this utility will automagically
  140.          fix the root entry.
  141.  
  142.         -Check the /proc entry. It needs to be there.
  143.  
  144.         -Check for minimally a swap space. If there is none
  145.          the user will be prompt to create one. The user
  146.          will have the choice to say "No don't bother to
  147.          ask again, I don't need a swap". A special comment
  148.          line will be added to /etc/fstab to prevent
  149.          linuxconf from asking again and again.
  150.     */
  151.     int swap_seen = 0;
  152.     int proc_seen = 0;
  153.     int no_swap_seen = 0;
  154.     char status[1000];
  155.     status[0] = '\0';
  156.     for (int i=0; i<getnb(); i++){
  157.         FSTAB_ENTRY *e = getitem(i);
  158.         e->check();
  159.         const char *mpoint = e->getmpoint();
  160.         if (strcmp(mpoint,"/")==0){
  161.             fixroot (e,status);
  162.         }else if (strcmp(mpoint,"/proc")==0){
  163.             proc_seen = 1;
  164.         }else if (e->is_swap()){
  165.             swap_seen = 1;
  166.         }else if (strcmp(e->getcomment(),NO_SWAP_NEEDED)==0){
  167.             no_swap_seen = 1;
  168.         }
  169.     }
  170.     if (!proc_seen){
  171.         strcat (status,MSG_U(N_PROCADD
  172.             ,"An entry for the /proc file system"
  173.              " has been added\n"
  174.              "The proc file system is needed for quite a few\n"
  175.              "system utilities such as \"ps\"\n\n"));
  176.         add (new FSTAB_ENTRY("none /proc proc defaults"));
  177.     }
  178.     if (!swap_seen && !no_swap_seen){
  179.         if (xconf_yesno (MSG_U(T_NOSWAP,"No swap !!!")
  180.             ,MSG_U(Q_NOSWAP,"Your linux system is lacking\n"
  181.              "a swap file or swap partition\n"
  182.              "\n"
  183.              "This is a severe weakness\n"
  184.              "Do you want to create a swap now ?")
  185.             ,help_swap)
  186.             == MENU_YES){
  187.             edit (FSTAB_ENTRY_SWAP);
  188.         }else if (xconf_yesno(MSG_U(T_AREYOUSURE,"Are you sure")
  189.             ,MSG_U(Q_BUGYOU,"I will bug you about your system\n"
  190.              "lacking a swap every time I can\n"
  191.              "\n"
  192.              "Do you want me to shut off ?")
  193.             ,help_swap)
  194.             == MENU_YES){
  195.             add (new FSTAB_ENTRY(NO_SWAP_NEEDED));
  196.         }
  197.     }
  198.     if (status[0] != '\0'){
  199.         xconf_notice(MSG_U(N_SOMEMODS
  200.             ,"Some ajustements were made to the /etc/fstab\n"
  201.              "\n"
  202.              "%s"),status);
  203.     }
  204. }
  205.  
  206. void fstab_check()
  207. {
  208.     FSTAB fstab;
  209.     fstab.check();
  210.     if (fstab.was_modified()) fstab.write();
  211. }
  212.  
  213.